<?php
/**
 * Created by PhpStorm.
 * User: liuhongchuan
 * Date: 2018/6/14
 * Time: 18:28
 */
namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class GymTicketController extends ApiController
{
    /**
     * 体验券列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {

        $page = $request->input("page", 1);
        $limit = $request->input("limit", 10);

        if ($page <= 0) {
            return $this->failed("参数错误");
        }
        if ($limit <= 0 || $limit > 100) {
            return $this->failed("参数错误");
        }
        $ticket_list = DB::table('gym_tickets')
            ->where(['status' => 1, 'is_delete' => 0])
            ->where(['remain_count', '>', 0])
            ->select('id', 'name', 'pic', 'start_time', 'end_time')
            ->skip($limit * ($page -1))
            ->take($limit)
            ->get();
        $count =  DB::table('gym_tickets')->where(['status' => 1, 'is_delete' => 0])->count();
        return $this->success(['ticket_list' => empty($ticket_list) ? [] : $ticket_list,  'more' => $count > $page * $limit ? true : false]);
    }

    /**
     * 体验券详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request) {
        $user = Auth::user();
        $user_id = $user->id;
        $id = $request->input('id');
        if ($id <= 0) {
            return $this->failed("参数错误");
        }

        $ticket = DB::table('gym_tickets AS t')
            ->leftJoin('gyms AS g', 'g.id', '=', 't.gym_id')
            ->where(['t.id' => $id, 't.status' => 1, 't.is_delete' => 0])
            ->where([['t.remain_count', '>', 0]])
            ->select('g.name as gym_name', 'g.desc', 'g.all_pics', 'g.addr', 'g.fee_scale', 't.id', 't.name', 't.pic', 't.start_time', 't.end_time', 't.rules')
            ->first();
        if (empty($ticket)) {
            return $this->failed("参数错误");
        }

        $is_get = DB::table('user_tickets')->where(['ticket_id' => $id, 'user_id' => $user_id])->first();

        return $this->success(['ticket' => $ticket, 'status' => empty($is_get) ? 0 : $is_get->status]);
    }


    /**
     * 体验券领取
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function get(Request $request)
    {
        $user = Auth::user();
        $user_id = $user->id;
        $id = $request->input('id');
        $company = $request->input('company');
        $position = $request->input('position');
        $wechat_no = $request->input('wechat_no');
        if ($id <= 0) {
            return $this->failed("参数错误");
        }
        if (empty($company)) {
            return $this->failed("公司名不能为空");
        }
        if (empty($position)) {
            return $this->failed("职位不能为空");
        }
        if (empty($wechat_no)) {
            return $this->failed("微信号不能为空");
        }
        $ticket = DB::table('gym_tickets AS t')->where(['t.id' => $id, 't.status' => 1, 't.is_delete' => 0])->first();
        if (empty($ticket)) {
            return $this->failed("参数错误");
        }

//        if ($ticket->remain_count <= 0 ) {
//            return $this->failed("健身券已领完");
//        }
        if ($ticket->start_time > time()) {
            return $this->failed("健身券还未到领取时间");
        }
        if ($ticket->end_time < time()) {
            return $this->failed("健身券已超过截止时间");
        }
        $is_get = DB::table('user_tickets')->where(['ticket_id' => $id, 'user_id' => $user_id])->first();
        if (!empty($is_get)) {
            return $this->failed("已报名，请勿重复领取");
        }

        DB::beginTransaction(); //开启事务

//        $ticket_res = DB::table('gym_tickets')->where(['id' => $id])->decrement('remain_count');

        $get_res = DB::table('user_tickets')->insert([
            'user_id' => $user_id,
            'ticket_id' => $id,
            'get_time' => time(),
            'company' => $company,
            'position' => $position,
            'wechat_no' => $wechat_no
        ]);

        if ($get_res === false) {
            DB::rollBack(); //回滚事务
            return $this->failed("报名失败");
        }
        DB::commit(); //提交事务
        return $this->success("报名成功");
    }
}